﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using CCF20Model;
using System.ComponentModel;
using WiseLink.RDS.Resource;

namespace CCF20Module
{
    /// <summary>
    /// Interaction logic for ContactCustomer.xaml
    /// </summary>
    public partial class ContactCustomer : Window
    {
        private Vehicle vehicle;
        private VehicleModel vehicleModel;
        
        public ContactCustomer(VehicleModel vmm, Vehicle vr)
        {
            InitializeComponent();
            vehicle = vr;
            vehicleModel = vmm;
            
        }

        private void Contact_Click(object sender, RoutedEventArgs e)
        {

            string strAction = ((Button)sender).Name;
            if (strAction == "btnPhoneContact")
            {
                ContactHistory contactHistory = new ContactHistory();
                contactHistory.ContactResult = this.comboContactResult.SelectedValue.ToString();
                contactHistory.IsActive = true;
                contactHistory.Remark = this.ContactMemo.Text;
                contactHistory.ContactTime = DateTime.Now;
                if (this.BookTime.SelectedValue != null)
                {
                    vehicle.BookTime = this.BookTime.SelectedValue;
                }
                else
                {
                    vehicle.BookTime = null;
                }
                vehicle.LastContactResult = this.comboContactResult.SelectedValue.ToString();
                vehicle.LastContactTime = DateTime.Now;
                contactHistory.Vehicle = vehicle;
                contactHistory.Operator = (string)Application.Current.Properties["CurrentUser"];
                //vehicleModel.AddContactHistoryForVehicle(vehicle, contactHistory);
                Utility.ShowMessageBox( Message.Save_Succeed);
                
                
            }
            else if (strAction == "btnSendSMS")
            {

                if (SMSTemplate.SelectedItem == null)
                {
                    return;
                }
                else
                {
                    SMSTemplate selectedTemplate = (CCF20Model.SMSTemplate)this.SMSTemplate.SelectedItem;
                    if (vehicle.FaultCount == 0 && vehicle.MaintenanceCount == 0 && vehicle.UrgentCount == 0)
                    {
                        //给车主发短信时，对于既没有故障、保养、撞车的车主，除了广告模板短信和索赔模板之外，其他是不允许发送成功的
                        if (SMSModel.NonFaultedCustomerShortMessageList.AsQueryable<string>().Count(s => s.Contains(selectedTemplate.SMSType)) == 0)
                        {
                            string strResult = string.Format(Message.Contact_Cant_SMS, vehicle.Customer.Name);
                            Utility.ShowMessageBox(strResult);
                            LoggerHelper.LogCCF20(strResult);
                            return;
                        }

                    }

                    string strMsg = SMSModel.SendShortMessage(vehicle, selectedTemplate,this.SMSTxt.Text);
                    Utility.ShowMessageBox(strMsg);

                    SMSModel.SendGPRSMessage(vehicle.Devices.FirstOrDefault().IDCSimNumber.Trim(),
                                             this.SMSTxt.Text.Trim(),
                                             selectedTemplate.SMSType);

                }
            }
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.comboContactResult.ItemsSource = WiseLink.RDS.Resource.ContactStatus.GetContactStatus();
            this.SMSTemplate.ItemsSource = SMSModel.GetSMSTemplate();
            this.BookTime.Visibility = System.Windows.Visibility.Hidden;
            DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(ComboBox.TextProperty, typeof(ComboBox));

            dpd.AddValueChanged(comboContactResult, OnTextChanged);
 

            
        }
        private void OnTextChanged(object sender, EventArgs args)
        {
            if (this.comboContactResult.Text.Contains(WiseLinkRDSStatus.Contact_WillRepair))
            {
                this.BookTime.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                this.BookTime.Visibility = System.Windows.Visibility.Hidden;
            }
        }

        private void SMSTemplate_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count==0) return;
            CCF20Model.SMSTemplate myTemplate = (CCF20Model.SMSTemplate)e.AddedItems[0];
            string smstext = SMSModel.FormatSMS(myTemplate.SMSText, vehicle,null,true);
            this.SMSTxt.Text = smstext;

        } 

        
    }
}
